package cn.stylefeng.guns.czlm.controller;

import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.czlm.entity.TbCategory;
import cn.stylefeng.guns.czlm.service.TbCategoryService;
import cn.stylefeng.guns.czlm.warpper.TbCategoryWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: LiJiaJun
 * @Date: 19-4-28 下午 03:54
 * @Version 1.0
 * 商品类目
 */
@Controller
@RequestMapping("/leimu")
public class LeiMuController extends BaseController {

    private String PREFIX = "/modular/system/category";

    String src=null;

    /**
     * 调用类目Service层
     */
    @Autowired
    private TbCategoryService tbCategoryService;

    /**
     * 商品类目跳转
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/leimu.html";
    }

    /**
     * 弹出添加窗口
     * @return
     */
    @RequestMapping("/leimu_add")
    public String leimu_add() {
        return PREFIX + "/leimu_add.html";
    }

    /**
     * 弹出修改窗口并传入id
     * @param id
     * @return
     */
    @RequestMapping("/leimu_update")
    public String leimu_update(@RequestParam Long id){
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        //把id传到修改页面
        TbCategory tbCategory= tbCategoryService.getById(id);
        LogObjectHolder.me().set(tbCategory);
        return PREFIX + "/leimu_edit.html";
    }

    /**
     * 分页查询数据
     * @return
     */
    @RequestMapping(value = "/findTbCat")
    @ResponseBody
    public Object findTbCat(){
        Page<Map<String, Object>> CatList = this.tbCategoryService.findTbCategory();
        Page<Map<String, Object>> wrap = new TbCategoryWrapper(CatList).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }


    /**
     * 删除类目方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam Long id){
        tbCategoryService.delCat(id);
        return SUCCESS_TIP;
    }

    /**
     * 添加类目方法
     * @param tbCat
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseData add(TbCategory tbCat){
        tbCat.setIcon(src);
        tbCategoryService.addCat(tbCat);
        return SUCCESS_TIP;
    }

    /**
     * 传入id到修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/findId")
    @ResponseBody
    public Object findId(@RequestParam Long id){
        TbCategory tbCategory=this.tbCategoryService.getById(id);
        return tbCategory;
    }

    /**
     * 保存修改的数据
     * @param tbCategory
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public ResponseData upd(TbCategory tbCategory){
        tbCategory.setIcon(src);
        tbCategoryService.updateById(tbCategory);
        return SUCCESS_TIP;
    }

    /**
     * 查询类别为1的值
     * @return
     */
    @RequestMapping(value = "/upLevelList")
    @ResponseBody
    public Object upLevelList(){
        return this.tbCategoryService.findLevel();
    }

    /**
     * 查询类别为2的值
     * @return
     */
    @RequestMapping(value = "/upLevelLists")
    @ResponseBody
    public Object upLevelLists(){
        return this.tbCategoryService.findLevels();
    }

    /**
     * 查询类目下商品数量
     * @return
     */
    @RequestMapping(value = "/Lists")
    @ResponseBody
    public List<Lists> Lists(){
        return this.tbCategoryService.findLists();
    }

    /**
     * 图片加载到项目
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/img")
    @ResponseBody
    public ResponseData upload(MultipartFile file) throws IOException {
        String sjs = UUID.randomUUID()+"";
        String newName=sjs+file.getOriginalFilename();
        FileCopyUtils.copy(file.getBytes(), new File("E:\\exploitTool\\IDEA\\guns\\src\\main\\webapp\\leimu\\" +newName));
        src = "leimu/" +newName;
        return SUCCESS_TIP;
    }
}